package com.sj.purchase.modules.stages.web;

import com.sj.purchase.modules.stages.model.StagesOrder;
import com.sj.purchase.modules.stages.repository.StagesOrderRepository;
import com.sj.purchase.util.Date2StrUtils;
import com.sj.purchase.util.ExcelDownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangrd
 * @date 2018/8/11
 **/
@RestController
@RequestMapping("api/stages-order")
public class StagesOrderController {

    private StagesOrderRepository repository;

    @Autowired
    public StagesOrderController(StagesOrderRepository repository) {
        this.repository = repository;
    }

    @GetMapping
    public Page<StagesOrder> findAll(Date start, Date end, Pageable pageable) {
        return repository.findAllByOrderDateBetweenOrderByOrderDateDesc(start, end, pageable);
    }

    @GetMapping("/excel")
    public void exportExcel(Date start, Date end) {
        String excelName = String.format("%s--%s %s", format(start), format(end), "订单分期");
        List<StagesOrder> list = repository.findAllByOrderDateBetweenOrderByOrderDateDesc(start, end, null).getContent().stream().collect(Collectors.toList());
        ExcelDownloadUtil.easyPoi(excelName, StagesOrder.class, list);
    }

    private String format(Date date){
        return Date2StrUtils.str(date).replaceAll("/","-");
    }
}
